﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.Odbc;


namespace CS3230HealthTrackerSystemGUI
{
    public partial class AdminQueryForm : Form
    {
        //Holds the Class the will generate the connection string, and connection string variable.
        private Decryption key = new Decryption();
        private String MyConString;

        public AdminQueryForm()
        {
            InitializeComponent();
            //Generates the connection string.
            
        }

        private void button1_Click(object sender, EventArgs e)
        {
            String checkText = queryTextEntry.Text.ToLower();
           
            //Holds the values to be stored into the dataGridview to be displayed.
            List<StringHolder> list = new List<StringHolder>();

            //Opens the connection string, and opens it.
            MyConString = key.returnConnectionString("xxjrmy", "xxjrmy", "xx3kz4z");
            OdbcConnection MyConnection = new OdbcConnection(MyConString);
            MyConnection.Open();



            //If there are no SQL syntax error executes.
            try
            {

                /*
                 * Error checking for the admin SQL statement.
                 * If the user tries to press go and has not entere text or a proper sql statement, it will display the error message.
                 * Otherwise the SQL statement will be run and checked later on.
                 * 
                 */
                if (queryTextEntry.Text.Equals(""))
                {
                    MessageBox.Show("Please enter a proper SQL statement.");
                }

                else
                {
                    //Executes the query from the text box.
                    OdbcCommand MyCommand = new OdbcCommand(queryTextEntry.Text, MyConnection);
                    OdbcDataReader MyDataReader;




                    if (checkText.Contains("drop"))
                    {
                        throw new ArgumentException("User access restricted : " + "\n" + "Unable to DROP a table.");
                    }

                    MyDataReader = MyCommand.ExecuteReader();

                    while (MyDataReader.Read())
                    {
                        //Creates a string to hold the values from the query.
                        String builder = " ";
                        //Checks the number of columns that are going to be readin, and for each column.
                        int values = MyDataReader.VisibleFieldCount;
                        for (int currentColumn = 0; currentColumn < values; currentColumn++)
                        {
                            //If the value in the current column is null.
                            if (MyDataReader.IsDBNull(currentColumn) == true)
                            {
                                builder += " " + MyDataReader.GetName(currentColumn) + " : " + " NULL ";
                            }
                            else
                            {
                                //Checks for the type of data in the column, and handles it appropriately.
                                //For each it checks the type, gets the column name, and then the value.
                                String type = MyDataReader.GetDataTypeName(currentColumn);

                                if (type == "varchar")
                                {
                                    builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetString(currentColumn).ToString() + " ";
                                }
                                if (type == "integer")
                                {
                                    builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetInt32(currentColumn).ToString() + " ";
                                }
                                if (type == "decimal")
                                {
                                    builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetDecimal(currentColumn).ToString() + " ";
                                }
                                if (type == "date")
                                {
                                    builder += " " + MyDataReader.GetName(currentColumn) + " : " + MyDataReader.GetDate(currentColumn).ToShortDateString() + " ";
                                }
                            }
                        }
                        //Stores the string in the string wrapper.
                        StringHolder currentString = new StringHolder(builder);
                        //Adds the string to be added into the data grid view.
                        list.Add(currentString);

                    }
                    //Assigns the data grid view the list of strings and then displays them on the GUI
                    queryResult.DataSource = list;
                    queryResult.Show();
                    //Closes the data reader.
                    MyDataReader.Close();
                }
            }

            //If there is an SQL syntax error, catches the error and prints to a message Box. 
            catch (System.Data.Odbc.OdbcException exception)
            {
                MessageBox.Show("There was an error in your SQL syntax: " + "\n" + exception.Message);
            }

            catch (Exception otherException)
            {
                MessageBox.Show(otherException.Message);
            }

            //Close all resources
            MyConnection.Close();
        }

    }
}
